//利用子进程去运行爬虫脚本
//开启一个child的子进程去启动爬虫脚本 并且监听爬虫脚本运行的情况
const cp = require('child_process');
const { resolve } = require('path');

;(async ()=>{
    const script = resolve(__dirname,'../crawler/video.js');
    const child = cp.fork(script,[]);
    //判断进程是否调用过
    let invoked = false;

    child.on('error',err => {
        if(invoked){

            return

        }else{
            invoked = true;
        }
        console.log(error)
    })

    child.on('exit',code => {
        if(invoked){

            return

        } else {
            
            invoked = false;
            let err = code === 0 ? null : new Error('exit code' + code)

            console.log(err)
        }
    })

    child.on('message',data => {
        console.log(data);
    })




})()